﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Text.RegularExpressions;
using System.IO;
using System.Media;
using System.Reflection;

namespace BAL
{
    public partial class ICStockEntry : Form
    {
        private LoginService.BarcodeJudgeServices bjs = new BAL.LoginService.BarcodeJudgeServices();
        Socket cliSocket = null;
        string ipAddr = "192.168.1.211";
        int port = 1024;

        private string FBrNo;
        private string FEntryNo;
        private string PkgsBarCode;
        private string PlaceBarCode;
        private string PlaceName;
        private string PkgsIsRegular;
        private string ID;

        //public ICStockEntry()
        //{
        //    InitializeComponent();
        //}

        //public ICStockEntry(string fno)
        //{           
        //    InitializeComponent();
        //    FBrNo = fno;
        //    this.tb_FBrNo.Text = FBrNo.Trim();
        //}

        public ICStockEntry(string fno,string _ID,out int num)
        {
            InitializeComponent();
            FBrNo = fno;
            num = 0;
          
            DataSet ds = bjs.GetICStockBillEntry(FBrNo);
            if (ds.Tables[0].Rows.Count > 0)
            { num = 1; }
            else { num = 0; } 
                    

            this.tb_FBrNo.Text = FBrNo.Trim();
            this.ID = _ID;
        }

        private void SetValue()
        {
            this.tb_FEntryNo.Text = "";
            this.tb_PkgsBarCode.Text = "";
            this.tb_PlaceBarCode.Text = "";
            this.tb_PlaceName.Text = "";
            this.comboBox1.Text = null;
            this.tb_PkgsBarCode.Focus();
        }

        private void ICStockEntry_Load(object sender, EventArgs e)
        {
            
            if (FBrNo == null || FBrNo == "")
            {
            }
            else 
            {
                this.tb_PkgsBarCode.Focus();
                DataGridStyle();
               

                BinData(); 
            }
        }

        //获取数据
        private void BinData()
        {
            DataSet ds = bjs.GetICStockBillEntry(FBrNo);
            DataTable dt = new DataTable();
            
            dt = ds.Tables[0];
            //if (dt.Rows.Count <= 0)
            //{
            //    return;
            //}
            //else
            //{
                dataGrid1.DataSource = dt;
                //查询已扫描
             DataSet ds1 = bjs.GetICStockBillcount(FBrNo);
            if (ds1.Tables[0].Rows.Count > 0)
            {
                textcount.Text = ds1.Tables[0].Rows[0]["zcount"].ToString();
            }
                
             txtzcoun.Text = dt.Rows.Count.ToString();
            //}
        }
        //数据窗口样式
        public void DataGridStyle()
        {
            DataGridTableStyle dts = new DataGridTableStyle();
            dataGrid1.TableStyles.Clear();
            dts.MappingName = "ICStockEntry";

            DataGridTextBoxColumn cl1 = new DataGridTextBoxColumn();
            cl1.HeaderText = "流水号";
            cl1.MappingName = "FEntryNo";//FEntryNo
            cl1.Width = 0;
            dts.GridColumnStyles.Add(cl1);

            DataGridTextBoxColumn cl2 = new DataGridTextBoxColumn();
            cl2.HeaderText = "包装码";
            cl2.MappingName = "PkgsBarCode";
            cl2.Width = 80;
            dts.GridColumnStyles.Add(cl2);

            DataGridTextBoxColumn cl3 = new DataGridTextBoxColumn();
            cl3.HeaderText = "仓位码";
            cl3.MappingName = "PlaceBarCode";
            cl3.Width = 80;
            dts.GridColumnStyles.Add(cl3);

            DataGridTextBoxColumn cl4 = new DataGridTextBoxColumn();
            cl4.HeaderText = "仓位名";
            cl4.MappingName = "PlaceName";
            cl4.Width = 80;
            dts.GridColumnStyles.Add(cl4);

            DataGridTextBoxColumn cl6 = new DataGridTextBoxColumn();
            cl6.HeaderText = "类别";
            cl6.MappingName = "PkgsIsRegular";
            cl6.Width = 50; 
            dts.GridColumnStyles.Add(cl6);

            dataGrid1.TableStyles.Add(dts);
        }



        private void dataGrid1_CurrentCellChanged(object sender, EventArgs e)
        {
            DataGrid dataGrid1 = sender as DataGrid;
            int index = ((DataGrid)sender).CurrentCell.RowNumber;
            if (((DataTable)(dataGrid1.DataSource)).Rows.Count > 0)
            {
                ((DataGrid)sender).Select(index);
            }
            FEntryNo = ((DataTable)(dataGrid1.DataSource)).Rows[index][0].ToString();
            PkgsBarCode = ((DataTable)(dataGrid1.DataSource)).Rows[index][1].ToString();
            PlaceBarCode = ((DataTable)(dataGrid1.DataSource)).Rows[index][2].ToString();
            PlaceName = ((DataTable)(dataGrid1.DataSource)).Rows[index][3].ToString();
            PkgsIsRegular = ((DataTable)(dataGrid1.DataSource)).Rows[index]["PkgsIsRegular"].ToString();

            this.tb_FBrNo.Text = FBrNo.Trim();
            this.tb_FEntryNo.Text = FEntryNo.Trim();
            this.tb_PkgsBarCode.Text = PkgsBarCode; 
            this.tb_PlaceBarCode.Text = PlaceBarCode; 
            this.tb_PlaceName.Text = PlaceName;        
            this.comboBox1.Text = PkgsIsRegular;
           

            this.tb_PkgsBarCode.Focus();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ICStock ics = new ICStock(this.ID);
            ics.Show();
            this.Hide();
        }

        private void tb_PkgsBarCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                //this.tb_PlaceBarCode.Focus();


                PkgsBarCode = this.tb_PkgsBarCode.Text.Trim();
                if(PkgsBarCode==null||PkgsBarCode=="")
                {
                    MessageBox.Show("包装码不存在，请重新扫描包装码！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3); 
                    return;
                }      
                FEntryNo ="";PlaceBarCode="";PkgsIsRegular="";PlaceName="";
                bool result1 = bjs.GetICStockBillEntryCode(PkgsBarCode, FBrNo, out  FEntryNo, out  PlaceBarCode, out PlaceName, out  PkgsIsRegular);//
                if (result1)                           
                {
                    this.tb_FEntryNo.Text = FEntryNo;
                    this.tb_PkgsBarCode.Text = PkgsBarCode;
                    //this.tb_PlaceBarCode.Text = PlaceBarCode;
                    //this.tb_PlaceName.Text = PlaceName;
                    this.comboBox1.Text = PkgsIsRegular;
                    this.tb_PlaceBarCode.Focus();
                }
                else
                {
                    MessageBox.Show("包装码不存在！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                    SetValue();
                    return;                   
                }
            }
        }
        private void tb_PlaceBarCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                PlaceBarCode = this.tb_PlaceBarCode.Text;
                //仓位码是否是数字
                Regex regex = new Regex(@"^(-?[0-9]*[.]*[0-9]*)$");
                Match result = regex.Match(Convert.ToString(PlaceBarCode));
                if (!result.Success)
                {
                    MessageBox.Show("仓位码只能输入数字！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                    tb_PlaceBarCode.Focus();
                    return;
                }
                string FNumber = "";//仓位代码
                bool lb_return = bjs.GetPlaceName(PlaceBarCode, out FNumber, out PlaceName);

                if (lb_return)
                {
                    this.tb_PlaceName.Text = PlaceName;
                    this.button1.Focus();
                }
                else 
                {
                    MessageBox.Show("仓位码不存在！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                    tb_PlaceBarCode.Focus();
                    return;
                }                
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FEntryNo = this.tb_FEntryNo.Text.Trim();
            PkgsBarCode = this.tb_PkgsBarCode.Text.Trim();
            PlaceBarCode = this.tb_PlaceBarCode.Text.Trim();
            PkgsIsRegular = this.comboBox1.Text.Trim();

            if (FEntryNo == null || FEntryNo == "")
            {
                MessageBox.Show("请选择入库记录！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);                  
                return;
            }
            if (PkgsBarCode == null || PkgsBarCode == "") 
            {
                MessageBox.Show("请扫描包装码！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);                  
                return;
            }
            if (PlaceBarCode == null || PlaceBarCode == "")
            {
                MessageBox.Show("请扫描仓位码！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);                  
                return;
            }
            //仓位代码是否是数字
            Regex regex = new Regex(@"^(-?[0-9]*[.]*[0-9]*)$");
            Match result = regex.Match(Convert.ToString(PlaceBarCode));
            if (!result.Success)
            {
                MessageBox.Show("仓位码只能输入数字！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                tb_PlaceBarCode.Focus();
                return;
            }
            //判断仓位代码是否存在
            string FNumber = "";//仓位代码
            bool lb_return1 = bjs.GetPlaceName(PlaceBarCode, out FNumber, out PlaceName);
            if (!lb_return1)
            {
                MessageBox.Show("仓位码不存在！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                tb_PlaceBarCode.Focus();
                return;
            }  
            bool lb_return = bjs.UpdateICStockEntry(FEntryNo, PkgsBarCode, PlaceBarCode, PkgsIsRegular);
            if (lb_return)
            {
                //SetValue();
                //语音读取成功
                string location = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase.ToString());
                string path = location + "\\ysm.wav";
                SoundPlayer player = new SoundPlayer();
                player.SoundLocation = path;
                player.Load();
                player.Play();
                //----------------
                this.tb_FEntryNo.Text = "";
                this.tb_PkgsBarCode.Text = "";
                this.tb_PlaceBarCode.Text = "";
                this.tb_PlaceName.Text = "";
                this.comboBox1.Text = null;
                this.tb_PkgsBarCode.Focus();
                DataGridStyle();
                BinData();
                
            }
            else 
            {
                SetValue();
                MessageBox.Show("提交失败！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                return;
            }
        }
        //判断是否是输入数字
      

    }
}